package com.bafa.dbcache;

import com.bafa.jdbc.DBAccess;
import com.bafa.jdbc.QueryObject;
import com.bafa.objects.Report;
import com.bafa.objects.schema.ObjectFactory;
import com.bafa.objects.schema.Team;
import com.bafa.objects.schema.TeamDetails;
import com.bafa.util.comparators.TeamComparator;

import java.io.StringReader;

import java.sql.ResultSet;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.TreeMap;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

public class GrabReports {

    private static HashMap reports = new HashMap();
    private static HashMap seasonReports = new HashMap();
    private static HashMap teamReports = new HashMap();
    private static HashMap matchReports = new HashMap();
    
    
    static void grabReports(DBAccess db) throws Exception {
        //*
        ResultSet reportResults = db.retrieveReports();
        while (reportResults.next()) {
            try {
                Report reportBean = new Report();
                String report = new String(reportResults.getBytes("report"));
                reportBean.setReport(report);
                reportBean.setMatchId(reportResults.getInt("matchId"));
                reportBean.setTeamId(reportResults.getInt("teamid"));
                reportBean.setReportId(reportResults.getString("id"));
                reportBean.setSeasonId(reportResults.getInt("seasonid"));
                reportBean.setAuthor(reportResults.getString("author"));
                reportBean.setTitle(reportResults.getString("title"));
                reportBean.setCategory(reportResults.getString("category"));
                
               addReports(reportBean);
                
                
            } catch (Exception ioe) {
                ioe.printStackTrace();
            }

        } 

    }
    
    
    public static void addReports(Report reportBean){
   
        if(reportBean.getSeasonId()>0){
           
            if(seasonReports.containsKey(reportBean.getSeasonId())){
                ArrayList seasonList = (ArrayList) seasonReports.get(reportBean.getSeasonId());
                seasonList.add(reportBean.getReportId());
                seasonReports.put(reportBean.getSeasonId(),seasonList);
            }else{
                ArrayList seasonList = new ArrayList();
                seasonList.add(reportBean.getReportId());
                seasonReports.put(reportBean.getSeasonId(),seasonList);
            }
        }
        
        if(reportBean.getTeamId()>0){
            
            if(teamReports.containsKey(reportBean.getTeamId())){
                ArrayList teamList = (ArrayList) teamReports.get(reportBean.getTeamId());
                teamList.add(reportBean.getReportId());
                teamReports.put(reportBean.getTeamId(),teamList);
            }else{
                ArrayList teamList = new ArrayList();
                teamList.add(reportBean.getTeamId());
                teamReports.put(reportBean.getTeamId(),teamList);
            }
        }
        
        if(reportBean.getMatchId()>0){
            
            if(matchReports.containsKey(reportBean.getMatchId())){
                HashMap matchList = (HashMap) matchReports.get(reportBean.getMatchId());
                matchList.put(reportBean.getTeamId(),reportBean.getReportId());
                matchReports.put(reportBean.getMatchId(),matchList);
                
            }else{
                HashMap matchList = new HashMap();
                matchList.put(reportBean.getTeamId(),reportBean.getReportId());
                matchReports.put(reportBean.getMatchId(),matchList);
                
            }
        }
        
        reports.put(reportBean.getReportId(),reportBean);
        
    }


    public void setReports(HashMap reports) {
        this.reports = reports;
    }

    public static HashMap getReports() {
        return reports;
    }

    public void setSeasonReports(HashMap seasonReports) {
        this.seasonReports = seasonReports;
    }

    public static HashMap getSeasonReports() {
        return seasonReports;
    }

    public void setTeamReports(HashMap teamReports) {
        this.teamReports = teamReports;
    }

    public static HashMap getTeamReports() {
        return teamReports;
    }

    public void setMatchReports(HashMap matchReports) {
        this.matchReports = matchReports;
    }

    public static HashMap getMatchReports() {
        return matchReports;
    }
}
